package com.xhwl.centiotdata.utils;

import java.io.*;

/**
 * @Description: 读取本地json文件
 * @Copyright: Copyright (c) 2021  ALL RIGHTS RESERVED.
 * @Company: 深圳市兴海物联科技有限公司
 * @Author: gyc
 * @CreateDate: 2021/10/18 11:22
 * @UpdateDate: 2021/10/18 11:22
 * @UpdateRemark: init
 * @Version: 1.0
 */
public class ReadJsonUtils {

    public static String readJsonFile(String path) {
        String jsonStr = "";
        try {
            File jsonFile = new File(path);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 读取本地json文件
     * @param fileName 放置在resources的json文件，例如：fileName=env.json
     * @return
     */
    public static String getJson(String fileName) {
        String path = ReadJsonUtils.class.getClassLoader().getResource(fileName).getPath();
        String json = readJsonFile(path);
        return json;
    }


}
